using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CNodeLabelList : List<CNodeLabel>
    {
        #region Constructors
        //Basic constructor
        public CNodeLabelList() : base() {}
       
        //More efficient memory-allocation if size is known
        public CNodeLabelList(int capacity) : base(capacity) {}
        
        //Shares the index (if its already been computed)
        public CNodeLabelList(CNodeLabelList list) : base(list.Count)
        {
            foreach (CNodeLabel i in list)
                base.Add(i);
        }

        //Generic list (eg. from paging control), have to assume type
        public CNodeLabelList(IList list) : base(list.Count)
        {
            foreach (CNodeLabel i in list)
                base.Add(i);
        }
        #endregion

        #region Top/Bottom/Page
        public CNodeLabelList Top(int count)
        {
            if (count >= this.Count)
                return this;
            return Page(count, 0); 
        }
        public CNodeLabelList Bottom(int count)   
        {
            if (count > this.Count)
                count = this.Count;
            return new CNodeLabelList(this.GetRange(this.Count - count - 1, count));
        }
        public CNodeLabelList Page(int pageSize, int pageIndex)
        {
            return new CNodeLabelList( CUtilities.Page(this, pageSize, pageIndex) );
        }
        #endregion

        #region BulkEditLogic
        public bool HaveSameValue(string propertyName)               { return CReflection.HaveSameValue(this, propertyName); }
        public void SetSameValue( string propertyName, object value) {        CReflection.SetSameValue( this, propertyName, value); }
        #endregion

        #region SortBy
        //Public
        public CNodeLabelList SortBy(string propertyName) { return SortBy(propertyName, false); }
        public CNodeLabelList SortBy(string propertyName, bool descending)
        {
            CNodeLabelList copy = new CNodeLabelList(this);
            if (this.Count == 0)    return copy;
            copy.Sort(new CNodeLabelList_SortBy(propertyName, descending, this));
            return copy;
        }
        //Private 
        private class CNodeLabelList_SortBy : CReflection.GenericSortBy, IComparer<CNodeLabel>
        {
            public CNodeLabelList_SortBy(string propertyName, bool descending, IList list) : base(propertyName, descending, list) { }
            public int Compare(CNodeLabel x, CNodeLabel y) { return base.Compare(x, y); }
        }
        #endregion

        #region SaveAll/DeleteAll
        //Use default connection (may be overridden in base class)
        public void SaveAll()   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc);    }    }
        public void DeleteAll() {   if (this.Count > 0) { DeleteAll(this[0].DataSrc);    }    }

        //Use connection supplied
        public void SaveAll(  CDataSrc dataSrc) {   dataSrc.BulkSave(this);    }
        public void DeleteAll(CDataSrc dataSrc) {   dataSrc.BulkDelete(this);  }

        //Use transaction supplied
        public void SaveAll(  IDbTransaction txOrNull)    {   foreach (CNodeLabel i in this) {   i.Save(  txOrNull);   }   }
        public void DeleteAll(IDbTransaction txOrNull)    {   foreach (CNodeLabel i in this) {   i.Delete(txOrNull);   }   }

        //Use a specified isolation level
        public void SaveAll(  IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { SaveAll(  this[0].DataSrc, txIsolationLevel);  }    }
        public void DeleteAll(IsolationLevel txIsolationLevel)   {   if (this.Count > 0) { DeleteAll(this[0].DataSrc, txIsolationLevel);  }    }

        //Use a specified connection and isolation level
        public void SaveAll(  CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkSave(  this, txIsolationLevel);  }
        public void DeleteAll(CDataSrc dataSrc, IsolationLevel txIsolationLevel)   {   dataSrc.BulkDelete(this, txIsolationLevel);  }
        #endregion
        
        #region Cache-Control
    
        //Supplementary List Overloads
        public void Add(   IList<CNodeLabel> itemsToAdd)    {   foreach (CNodeLabel i in itemsToAdd)   { Add(   i); }   }
        public void Remove(IList<CNodeLabel> itemsToRemove) {   foreach (CNodeLabel i in itemsToRemove){ Remove(i); }   }
        #endregion  

        #region Main Index (on LabelNodeGuid, LabelTagId)
        public CNodeLabel GetById(Guid labelNodeGuid, int labelTagId)
        {
            CNodeLabel c = null;
            string k = Key(labelNodeGuid, labelTagId);
            Index.TryGetValue(k, out c);
            return c;
        }
        public static string Key(Guid labelNodeGuid, int labelTagId)
        {
            return String.Concat(labelNodeGuid, "_", labelTagId);
        }

        [NonSerialized]
        private Dictionary<string,CNodeLabel> _index;
        private Dictionary<string,CNodeLabel> Index
        {
            get    
            {
                if (null != _index)
                    if (_index.Count == this.Count)
                        return _index;

                _index = new Dictionary<string,CNodeLabel>(this.Count);
                foreach (CNodeLabel i in this)
                    _index[ Key(i.LabelNodeGuid, i.LabelTagId) ] = i;
                return _index;
             }
        }
        #endregion
            
        #region Foreign-Key Indices (Subsets)
        //Index by LabelNodeGuid
        public CNodeLabelList GetByNodeGuid(Guid nodeGuid)
        {
            CNodeLabelList temp = null;
            if (! IndexByNodeGuid.TryGetValue(nodeGuid, out temp))
            {
                temp = new CNodeLabelList();
                IndexByNodeGuid[nodeGuid] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<Guid, CNodeLabelList> _indexByNodeGuid;
        private Dictionary<Guid, CNodeLabelList> IndexByNodeGuid
        {
            get
            {
                if (null == _indexByNodeGuid)
                {
                    Dictionary<Guid, CNodeLabelList> index = new Dictionary<Guid, CNodeLabelList>();
                    CNodeLabelList temp = null;
                    foreach (CNodeLabel i in this)
                    {
                        if (! index.TryGetValue(i.LabelNodeGuid, out temp))
                        {
                            temp = new CNodeLabelList();
                            index[i.LabelNodeGuid] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByNodeGuid = index;
                }
                return _indexByNodeGuid;
            }
        }
        //Index by LabelTagId
        public CNodeLabelList GetByTagId(int tagId)
        {
            CNodeLabelList temp = null;
            if (! IndexByTagId.TryGetValue(tagId, out temp))
            {
                temp = new CNodeLabelList();
                IndexByTagId[tagId] = temp;
            }
            return temp;
        }

        [NonSerialized]
        private Dictionary<int, CNodeLabelList> _indexByTagId;
        private Dictionary<int, CNodeLabelList> IndexByTagId
        {
            get
            {
                if (null == _indexByTagId)
                {
                    Dictionary<int, CNodeLabelList> index = new Dictionary<int, CNodeLabelList>();
                    CNodeLabelList temp = null;
                    foreach (CNodeLabel i in this)
                    {
                        if (! index.TryGetValue(i.LabelTagId, out temp))
                        {
                            temp = new CNodeLabelList();
                            index[i.LabelTagId] = temp;
                        }
                        temp.Add(i);
                    }
                    _indexByTagId = index;
                }
                return _indexByTagId;
            }
        }

        #endregion


    }
}